package zhongbo.java.air.ingestion.listener;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import zhongbo.java.air.ingestion.dto.FlightPathDto;

import java.util.Date;

@Component
public class RedisSubscribeChanelListener implements MessageListener {
    @Autowired
    private RedisChanelPublisher redisChanelPublisher;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String msg = new String(message.getBody());
       System.out.println("收到消息：" + msg);
        convert(msg);
    }

    private void convert(String msg) {
        FlightPathDto flightPathDto = new FlightPathDto();
        JSONObject jsonObject = new JSONObject(msg);
        System.out.println("收到消息JSON" + jsonObject);
        if (jsonObject.getStr("icao") != null) {
            if (jsonObject.getStr("icao").matches("\\w{4,6}")) {
                flightPathDto.setIcaoCode(jsonObject.getStr("icao"));
            }
        }
        JSONArray traceArray = jsonObject.getJSONArray("trace");
        if (traceArray != null) {
            String timestampStr = jsonObject.getStr("timestamp");
            Double timestamp = jsonObject.getDouble("timestamp");
            Object o = traceArray.get(0);
            if (o != null) {
                String oStr = String.valueOf(o);
                double oDouble = Double.parseDouble(oStr);
                if (oDouble >= 0) {
                    double finalTimeMs = timestamp + oDouble;
                    long finalTimeMsLong = (long) finalTimeMs;
                    Date date = new Date(finalTimeMsLong * 1000);
                    if (timestampStr.length() == 13) {
                        date = new Date(finalTimeMsLong);
                    }
                    flightPathDto.setTimestamp(date);
                }
            }

            try {
                Double latitude = traceArray.getDouble(1);
                flightPathDto.setLatitude(latitude);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Double longitude = traceArray.getDouble(2);
                flightPathDto.setLongitude(longitude);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (traceArray.get(3).toString().matches("ground")) {
                    flightPathDto.setAltitude(0.0);
                } else {
                    Double altitude = traceArray.getDouble(3);
                    flightPathDto.setAltitude(altitude);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Double speed = traceArray.getDouble(4);
                flightPathDto.setSpeed(speed);
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                Double heading = traceArray.getDouble(5);
                flightPathDto.setHeading(heading);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                JSONObject Type = traceArray.getJSONObject(8);
                String type = Type.getStr("type");
                flightPathDto.setSourceType(type);
            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("映射成功:" + flightPathDto);

            redisChanelPublisher.broadcast(flightPathDto);
        }
    }
}
